Imports System.Data.SqlClient
Imports System.Data
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class DocumentoAlmacenPickeadorDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function registrarDocumentoAlmacenPickeador(ByVal dap As DocumentoAlmacenPickeador) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "USP_RegistrarDocumentoAlmacenPickeador"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", dap.CIA)
                .Parameters.AddWithValue("@SUC", dap.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", dap.TDOC_Codigo)
                .Parameters.AddWithValue("@DCA", dap.DCA_Codigo)
                .Parameters.AddWithValue("@DAP", dap.DAP_Codigo)
                .Parameters.AddWithValue("@ALM", dap.ALM_Codigo)
                .Parameters.AddWithValue("@PICK", dap.PICK_Codigo)
                .Parameters.AddWithValue("@ACT", dap.DAP_Activo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function actualizarDocumentoAlmacenPickeador(ByVal dap As DocumentoAlmacenPickeador) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "USP_ActualizarDocumentoAlmacenPickeador"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", dap.CIA)
                .Parameters.AddWithValue("@SUC", dap.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", dap.TDOC_Codigo)
                .Parameters.AddWithValue("@DCA", dap.DCA_Codigo)
                .Parameters.AddWithValue("@DAP", dap.DAP_Codigo)
                .Parameters.AddWithValue("@ALM", dap.ALM_Codigo)
                .Parameters.AddWithValue("@PICK", dap.PICK_Codigo)
                .Parameters.AddWithValue("@DAPFASIG", dap.DAP_FechaAsig)
                .Parameters.AddWithValue("@DAPFFASIG", dap.DAP_FechaFinAsig)
                .Parameters.AddWithValue("@DAPORD", dap.DAP_Orden)
                .Parameters.AddWithValue("@ACT", dap.DAP_Activo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try
    End Function

    Function obtenerListadoDocumentoAlmacenPickeador(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDOC As String, ByVal codDCA As String) As DataSet
        Dim ds As New DataSet
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoAlmacenPickeador " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDOC AND DCA_Codigo = @DCA"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", codTDOC)
                .Parameters.AddWithValue("@DCA", codDCA)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            adapter.Fill(ds)
        Catch ex As Exception
            ds = Nothing
        Finally
            con.Close()
        End Try

        Return ds
    End Function

    Function obtenerDocumentoAlmacenPickeador(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDOC As String, ByVal codDCA As String, ByVal codDAP As String) As DocumentoAlmacenPickeador
        Dim dap As New DocumentoAlmacenPickeador("-1", "", "", "", "", "", "", "", "", "", "")
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoAlmacenPickeador " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDOC AND DCA_Codigo = @DCA AND DAP_Codigo = @DAP"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", codTDOC)
                .Parameters.AddWithValue("@DCA", codDCA)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                dap = New DocumentoAlmacenPickeador(ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, _
                                                    ds.Tables(0).Rows(0)("DCA_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DAP_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("ALM_Codigo").ToString.Trim, _
                                                    ds.Tables(0).Rows(0)("PICK_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DAP_FechaAsig").ToString.Trim, ds.Tables(0).Rows(0)("DAP_FechaFinAsig").ToString.Trim, _
                                                    ds.Tables(0).Rows(0)("DAP_Orden").ToString.Trim, ds.Tables(0).Rows(0)("DAP_Activo").ToString.Trim)
            End If
        Catch ex As Exception
            dap = Nothing
        Finally
            con.Close()
        End Try

        Return dap
    End Function

    Function eliminarDocumentoAlmacenPickeador(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDO As String, ByVal codDCA As String, ByVal codDAP As String) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "UPDATE Documento_Almacen_Pickeador " & _
                                  "SET DAP_Activo = '0' " & _
                                  "WHERE CIA = @CIA AND SUC = @SUC AND TDOC_Codigo = @TDO AND DCA_Codigo = @DCA AND DAP_Codigo = @DAP " & _
                                  "AND DAP_Activo = '1'"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDO", codTDO)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DAP", codDAP)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

End Class
